package cn.limeizi.shoppingcar;

import cn.limeizi.shoppingcar.beans.McBean;
import cn.limeizi.shoppingcar.beans.OrderItemBean;
import cn.limeizi.shoppingcar.beans.User;
import cn.limeizi.shoppingcar.daos.McDao;
import cn.limeizi.shoppingcar.daos.OrderItemDao;
import cn.limeizi.shoppingcar.daos.UserDao;
import cn.limeizi.shoppingcar.daos.impl.McDaoImpl;
import cn.limeizi.shoppingcar.daos.impl.OrderItemDaoImpl;
import cn.limeizi.shoppingcar.daos.impl.UserImpl;
import com.sun.org.apache.xpath.internal.operations.Or;

import java.util.*;

/**
 * @Author: 李美姿
 * @Date: 2019/7/11 10:29
 * @Version 1.0
 * @描述：购物车类
 */
public class ShoppingCar {

    private static double totalPrice;  //商品总称
    private static int totalCount;   //商品总数量
    private static User user;//初始化为null，没有登录

    private static McDao mcDao=new McDaoImpl();
    private static OrderItemDao orderItemDao=new OrderItemDaoImpl();

    public static Scanner scanner=new Scanner(System.in);

    /*
    * 主界面
    * */
    public static void main(String[] args) {
        System.out.println("*********欢迎使用购物车操作系统*********");
        while (true) {
            System.out.println("0.登录");
            System.out.println("1.购买商品");
            System.out.println("2.删除商品");
            System.out.println("3.查看购物车");
            System.out.println("4.修改商品");
            System.out.println("5.清空购物车");
            System.out.println("请选择要进行的操作对应的序号");
            /*
             * 选择操作
             * */
            int choose = scanner.nextInt();   //获取键盘进行的操作的值
            switch (choose) {
                case 0:
                    login();  //登录
                    break;
                case 1:
                    buyMc(1);  //购买商品
                    break;
                case 2:
                    delMc(1);  //删除商品
                    break;
                case 3:
                    showShoppingCar();  //查看购物车
                    break;
                case 4:
                    update(1,2);  //修改商品
                    break;
                case 5:
                    cleanShoppingCar();  //清空购物车
                    break;
                default:
                    break;
            }
        }
    }

    /*
     * 无参构造用于数据初始化
     * */
    public void ShoppingCar() {

    }

    /*
    * 用户登录
    * */
    public static void login(){
        UserDao userDao=new UserImpl();
        System.out.println("请输入用户名：");
        String userName=scanner.next();
        System.out.println("请输入密码：");
        String password=scanner.next();
        user=userDao.login(userName,password);
        if (user!=null){
            System.out.println("登录成功！");
        }else {
            System.out.println("登录失败！");
        }
    }

    /*
     * 添加购物车商品(购买)
     * */
    public static void buyMc(int nid) {  //传入商品id
        /*
         * 购买分两种情况：
         * 1.没有该商品，则添加
         * 2.有该商品，则修改
         * */
        System.out.println("***********购买界面***********");
        //判断是否登录
        if (user==null){
            System.out.println("请先登录！");
            return;
        }
        /*
        * 显示商品
        * */
        McDao mcDao=new McDaoImpl();
        List<McBean> mcBeanList=mcDao.getAllMc();
        System.out.println("商品编号  "+"  商品名称  "+"  单价  "+"     描述");
        for (McBean mcBean:mcBeanList) {
            System.out.println("   "+mcBean.getNid()+"\t\t "+mcBean.getSname()+"\t  "+mcBean.getNprice()+"\t   "+mcBean.getSdescription());
        }

        System.out.print("请输入商品id:");
        int shoppingId=scanner.nextInt();
        OrderItemDao orderItemDao=new OrderItemDaoImpl();
        //根据传入的商品id从数据库获取该商品是否买过
        OrderItemBean orderItemBean=orderItemDao.getOne(user.getId(),shoppingId);
        if (orderItemBean!=null) {  //判断商品在购物车是否存在
            //获得该订单项
            int oldNumber = orderItemBean.getMcNumber(); //获得旧数量
            oldNumber++;
            orderItemBean.setMcNumber(oldNumber);  //把修改的数量添加回
            orderItemBean.setUserId(user.getId());  //设置用户id
            int out=orderItemDao.updateOrderItem(orderItemBean);//更新到数据库
            if (out>0){
                System.out.println("购买成功！");
            }else {
                System.out.println("购买失败");
            }
        } else {  //商品在购物车不存在
            //新建一个订单项
            orderItemBean = new OrderItemBean();
            orderItemBean.setMcNumber(1);  //设置数量为1
            orderItemBean.setMcId(shoppingId);  //设置商品id
            orderItemBean.setUserId(user.getId());  //设置用户id
            //把订单放到购物车表里
            int out=orderItemDao.addOrderItem(orderItemBean);
            if (out>0){
                System.out.println("购买成功！");
            }else {
                System.out.println("购买失败");
            }
        }


    }

    /*
     * 删除购物车商品
     * */
    public static void delMc(int nid) {  //传入购物车id
        if (user==null){
            System.out.println("请先登录！");
            return;
        }
        System.out.println("***********删除商品***********");
        System.out.println("请输入要删除的购物车id");
        nid=scanner.nextInt();
        int out=orderItemDao.deleteOrderItem(nid);//把该商品条目移除,更新到数据库
        if (out>0){
            System.out.println("删除成功！");
        }else {
            System.out.println("删除失败");
        }
    }

    /*
     * 修改购物车商品
     * */
    public static void update(int nid, int number) {  //传入商品id和要修改的数量
        if (user==null){
            System.out.println("请先登录！");
            return;
        }
        System.out.println("***********修改商品***********");
        //通过id拿到该商品条目
        OrderItemBean orderItemBean = orderItemDao.getOne(user.getId(),nid);
        //直接更改数量
        System.out.println("请输入要修改的购物车的id：");
        nid=scanner.nextInt();
        orderItemBean.setItemId(nid);
        System.out.println("请输入要修改的数量：");
        number=scanner.nextInt();
        orderItemBean.setMcNumber(number);
        //更改完刷新数据
        int out=orderItemDao.updateOrderItem(orderItemBean);//把该商品条目移除,更新到数据库
        if (out>0){
            System.out.println("修改成功！");
        }else {
            System.out.println("修改失败");
        }
    }

    /*
     * 查看购物车商品信息
     * */
    public static void showShoppingCar() {
        //判断是否登录
        if (user==null){
            System.out.println("请先登录！");
            return;
        }
        System.out.println("***********购物车***********");
        //查询该用户购物车
        OrderItemDao orderItemDao=new OrderItemDaoImpl();
        McDao mcDao=new McDaoImpl();
        List<OrderItemBean> orderItemBeans=orderItemDao.getAll(user.getId());
        /*
         * 打印商品
         * */
        System.out.println("购物车id   商品名称    单价     数量     小计");
        totalPrice=0;//数据初始化
        totalCount=0;
        for (OrderItemBean orderItemBean:orderItemBeans) {//购物车id，商品id，数量，用户id
            McBean mcBean=mcDao.getMc(orderItemBean.getMcId());
            System.out.print("   "+orderItemBean.getItemId()+"    ");
            System.out.print("    "+mcBean.getSname()+"  ");
            System.out.print("    "+mcBean.getNprice()+"   ");
            System.out.print("    "+orderItemBean.getMcNumber()+"  ");
            System.out.print("    "+orderItemBean.getMcNumber()*mcBean.getNprice()+"   ");
            System.out.println();
            totalCount=totalCount+orderItemBean.getMcNumber();
            totalPrice=totalPrice+mcBean.getNprice()*orderItemBean.getMcNumber();
        }
        /*
        * 打印总价和总数量
        * */
        System.out.println("\t\t\t\t\t\t 总数量:"+totalCount+" 总价："+totalPrice);
    }

    /*
     * 清空购物车
     * */
    public static void cleanShoppingCar() {
        if (user==null){
            System.out.println("请先登录！");
            return;
        }
        System.out.println("***********清空购物车***********");
        int out=orderItemDao.clear(user.getId());//把该商品条目移除,更新到数据库
        if (out>0){
            System.out.println("清空购物车成功！");
        }else {
            System.out.println("清空购物车失败！");
        }

    }


}
